﻿using Shop.Collect.Linq;
using Shop.Model.Coupon;
using Shop.Model.DB;
using Shop.RemoteModel;
using Shop.RemoteModel.Coupon.Model;
using Shop.Repository;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Shop.Collect.lmpl
{
    internal class CouponCollect : ICouponCollect
    {
        private readonly ICouponRepository _Repository;

        public CouponCollect (ICouponRepository repository)
        {
            this._Repository = repository;
        }
        public DBShopCoupon Get (long id)
        {
            return this._Repository.Get(id);
        }
        public long Add (CouponSet data)
        {
            if (this._Repository.IsExist(c => c.CouponTitle == data.CouponTitle))
            {
                throw new ErrorException("shop.coupon.title.error");
            }
            CouponSetArg add = data.ConvertMap<CouponSet, CouponSetArg>();
            add.CouponShow = data.GetCouponShow();
            return this._Repository.Add(add);
        }
        public bool Set (DBShopCoupon coupon, CouponSet data)
        {
            if (coupon.Status != CouponStatus.起草)
            {
                throw new ErrorException("shop.coupon.not.allow.update");
            }
            if (coupon.CouponTitle != data.CouponTitle && this._Repository.IsExist(c => c.CouponTitle == data.CouponTitle))
            {
                throw new ErrorException("shop.coupon.title.error");
            }
            CouponSetArg set = data.ConvertMap<CouponSet, CouponSetArg>();
            set.CouponShow = data.GetCouponShow();
            return this._Repository.Update(coupon, set);
        }
        public Result[] Query<Result> (CouponQueryParam query, IBasicPage paging, out int count) where Result : class
        {
            return this._Repository.Query<Result>(query, paging, out count);
        }
        public void Delete (DBShopCoupon coupon)
        {
            if (coupon.Status != CouponStatus.起草)
            {
                throw new ErrorException("shop.coupon.not.allow.delete");
            }
            this._Repository.Delete(coupon.Id);
        }
        public bool Offshelf (DBShopCoupon coupon)
        {
            if (coupon.Status == CouponStatus.已下架)
            {
                return false;
            }
            if (coupon.Status != CouponStatus.启用)
            {
                throw new ErrorException("shop.coupon.status.error");
            }
            coupon.ActualEndTime = DateTime.Now;
            coupon.Status = CouponStatus.已下架;
            return this._Repository.Update(coupon, "ActualEndTime", "Status");
        }
        public bool Stop (DBShopCoupon coupon)
        {
            if (coupon.Status == CouponStatus.停用)
            {
                return false;
            }
            if (coupon.Status != CouponStatus.启用 || coupon.PutInBegin <= DateTime.Now)
            {
                throw new ErrorException("shop.coupon.status.error");
            }
            coupon.Status = CouponStatus.停用;
            return this._Repository.Update(coupon, "Status");
        }
        public bool Enable (DBShopCoupon coupon)
        {
            if (coupon.Status == CouponStatus.启用)
            {
                return false;
            }
            if (coupon.Status != CouponStatus.起草 && coupon.Status != CouponStatus.停用)
            {
                throw new ErrorException("shop.coupon.status.error");
            }
            coupon.Status = CouponStatus.启用;
            coupon.PutInStatus = CouponPutInStatus.准备中;
            return this._Repository.Update(coupon, "Status", "PutInStatus");
        }

        public void CheckStatus (long id, CouponStatus[] status)
        {
            var old = this._Repository.Get(id, c => new
            {
                c.Status
            });
            if (!status.Contains(old.Status))
            {
                throw new ErrorException("shop.coupon.status.change");
            }
        }
    }
}
